package com.evildog.workspace.invest.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.invest.application.bo.BillReportListBO;
import com.evildog.workspace.invest.application.bo.BillReportPageBO;
import com.evildog.workspace.invest.application.bo.BillReportStatBO;
import com.evildog.workspace.invest.application.convert.BillReportConvert;
import com.evildog.workspace.invest.application.dto.BillReportListDTO;
import com.evildog.workspace.invest.application.dto.BillReportPageDTO;
import com.evildog.workspace.invest.application.entity.BillReport;
import com.evildog.workspace.invest.application.entity.BillReportListDO;
import com.evildog.workspace.invest.application.entity.BillReportPageDO;
import com.evildog.workspace.invest.application.mapper.BillReportMapper;
import com.evildog.workspace.invest.application.service.BillReportService;
import com.evildog.workspace.invest.application.service.BillService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class BillReportServiceImpl extends ServiceImpl<BillReportMapper, BillReport> implements BillReportService {

    private BillService billService;

    @Override
    public PagedResult<BillReportPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, BillReportPageDTO pageDTO) {
        IPage<BillReportPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return BillReportConvert.INSTANCE.convert(page);
    }

    @Override
    public List<BillReportListBO> getList(List<QueryConditionDTO> queryCondition, BillReportListDTO dto) {
        List<BillReportListDO> billReportList = this.baseMapper.getList(queryCondition, dto);
        return BillReportConvert.INSTANCE.convert(billReportList);
    }

    @Override
    public BillReportStatBO getStatistics(Integer year) {
        BillReport statistics = this.baseMapper.getStatistics(year);

        return new BillReportStatBO()
                .setIncome(statistics.getTotalIncome())
                .setExpend(statistics.getTotalExpend())
                ;
    }

    @Override
    public void geneReport(int year, int month) {
        BillReportStatBO totalReport = billService.getTotalReport(year, month);

        BillReport billReport = new BillReport()
                .setMonth(month)
                .setYear(year)
                .setTotalIncome(totalReport.getIncome())
                .setTotalExpend(totalReport.getExpend());

        save(billReport);
    }

}
